package cn.wolfcode.service.impl;

import cn.wolfcode.domain.StrategyComment;
import cn.wolfcode.query.StrategyCommentQuery;
import cn.wolfcode.repository.StrategyCommentRepository;
import cn.wolfcode.service.IStrategyCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository strategyCommentRepository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyComment.setCreateTime(new Date());
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        return strategyCommentRepository.findById(id).orElse(null);

    }

    @Override
    public List<StrategyComment> findAll() {
        return strategyCommentRepository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        //strategyCommentRepository 查询只能对所有数据进行分页
        //template 查询不能直接对数据进行分页 间接分页

        Query query = new Query();

        if(StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("StrategyId").is(qo.getStrategyId()));
        }


        //totalCount 总记录数
        long totalCount = template.count(query, StrategyComment.class);
        if(totalCount == 0){
            return  Page.empty();  //空的分页对象
        }

        //分页参数对象, 参数1 当前页  2 每页显示的条数  3 排序规则  4 排序字段
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "createTime");

        query.with(pageRequest);

        List<StrategyComment> list = template.find(query, StrategyComment.class);

        return new PageImpl<>(list, pageRequest, totalCount);
    }

    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        if(StringUtils.hasLength(strategyComment.getId())){
            //有id修改
            //参数个数一致等同于全量更新
            this.update(strategyComment);

        }else{
            this.save(strategyComment);
        }
    }

    @Override
    public void commentThumb(String cid, String uid) {
        //查询用户对象  获取点赞用户集合
        StrategyComment comment = this.get(cid);
        //判断当前用户是否在集合中
        List<String> uidList = comment.getThumbuplist();
        if(!uidList.contains(uid)){
            //如果不在  点赞操作  +1   用户添加到集合中
            comment.setThumbupnum(comment.getThumbupnum() + 1);
            uidList.add(uid);
        }else{
            //在   取消点赞操作   删除集合中用户id
            comment.setThumbupnum(comment.getThumbupnum() - 1);
            uidList.remove(uid);
        }

        //更新
        this.update(comment);


    }

}
